package com.zeeephr.core.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.zeeephr.core.Servlet;
import com.zeeephr.core.bean.Clz;
import com.zeeephr.core.bean.ClzMapper;


public class Utils {
	private Map<String ,String> map1;
	private Map<String ,String> map2;
	
	List<Clz> clzs;
	List<ClzMapper>clzMappers;
	public Utils(ArrayList<Clz> clzs,ArrayList<ClzMapper> clzMappers){
		this.clzs=clzs;
		this.clzMappers=clzMappers;
		map1=new HashMap<>();
		map2=new HashMap<>();
		for(Clz c:clzs) {
			map1.put(c.getName(), c.getWhere());
			System.out.println("clz  "+c.getName()+"---->"+c.getWhere());
			
		}
		for(ClzMapper mapper:clzMappers) {
			for(String key:mapper.getUrl()) {
				map2.put(key, mapper.getName());
				System.out.println("mapper  "+key+"---->"+mapper.getName());
				
			}
		}
	}
	public String getPattern(String url) {
		String servletName=map2.get(url);
		return map1.get(servletName);
	}
	public Servlet getServletByURL(String url) {
		try {
			String pattern =getPattern(url);
			Class servletClass=Class.forName(pattern);
			Constructor<Servlet> constructor=servletClass.getConstructor();
			return (Servlet) constructor.newInstance();//通过反射 获取Servlet接口
		} catch (Exception e) {
			// TODO Auto-generated catch block
			System.out.println("找不到servlet类   注入失败");
			
			return null;
		}
		
	}
}
